﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web;
using Telerik.Web.UI;
using portfolio.Business;
using portfolio.Common;

namespace portfolio.Views.DancersDBViews
{
    public partial class DancersDBPlaylist : System.Web.UI.UserControl
    {
        bool isExport = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            InitializeSession();
        }

        #region Helper Methods
        private void InitializeSession()
        {
            Initialize_RadGrid(rgDanceLibraryData);
            Initialize_RadGrid(rgPlayListData);
        }

        private void Initialize_RadGrid(RadGrid grid)  // COME BACK AND FIX SESSION CONSTANTS
        {
            if (grid.ID == StringConstants.rgDanceLibraryData)
            {
                if (Session[SessionConstants.DancersDBLibraryData] == null)
                {
                    DancersDBBL dancersDBBL = new DancersDBBL();
                    Session[SessionConstants.DancersDBLibraryData] = dancersDBBL.GetAllSongs();
                    grid.DataSource = (List<DancersDBData>)Session[SessionConstants.DancersDBLibraryData];
                }
                else
                {
                    grid.DataSource = (List<DancersDBData>)Session[SessionConstants.DancersDBLibraryData];
                }
            }
            else if(grid.ID == StringConstants.rgPlayListData)
            {
                if (Session[SessionConstants.DancersDBPlayListData] == null)
                {
                    grid.DataSource = string.Empty;
                    grid.MasterTableView.NoMasterRecordsText = "Select songs to add to the playlist.";
                }
                else
                {
                    grid.DataSource = (List<DancersDBData>)Session[SessionConstants.DancersDBPlayListData];
                }
            }
        }

        private void Initialize_rcbDanceStyleFilter(RadGrid grid, string filterName)
        {
            DancersDBBL dancersDBBL = new DancersDBBL();

            foreach (GridFilteringItem item in grid.MasterTableView.GetItems(GridItemType.FilteringItem))
            {
                RadComboBox rcb = (RadComboBox)item.FindControl(filterName);
                rcb.DataSource = dancersDBBL.GetAllDanceStyles();
                rcb.DataBind();
                rcb.Items.Insert(0, new RadComboBoxItem("Select All Styles", StringConstants.selectAllValues));
            }
        }

        private void ItemCommand_RebindGrid(RadGrid grid, string filterName) // COME BACK AND FIX SESSION CONSTANTS
        {
            DancersDBBL dancersDBBL = new DancersDBBL();
            bool selectAllSongsChecked = false;

            foreach (GridFilteringItem item in grid.MasterTableView.GetItems(GridItemType.FilteringItem))
            {
                RadComboBox rcb = (RadComboBox)item.FindControl(filterName);
                List<string> danceStyles = new List<string>();

                if (rcb != null)
                {
                    if (rcb.CheckedItems.Count == 0 || rcb.SelectedIndex == 0)
                    {
                        if (grid.ID == StringConstants.rgDanceLibraryData) // Only the library needs filtering capabilities.
                        {
                            Session[SessionConstants.DancersDBLibraryData] = dancersDBBL.GetAllSongs();
                        }
                    }
                    else
                    {
                        foreach (RadComboBoxItem comboItem in rcb.CheckedItems)
                        {
                            selectAllSongsChecked = (comboItem.Value == StringConstants.selectAllValues);
                            if (selectAllSongsChecked) break;
                            danceStyles.Add(comboItem.Value);
                        }
                        if (grid.ID == StringConstants.rgDanceLibraryData) // Only the library needs filtering capabilities.
                        {
                            if (!selectAllSongsChecked)
                            {
                                Session[SessionConstants.DancersDBLibraryData] = dancersDBBL.GetAllSongsForDances(danceStyles);
                            }
                            else
                            {
                                Session[SessionConstants.DancersDBLibraryData] = dancersDBBL.GetAllSongs();
                            }
                        } 
                    } 
                }
            }
            grid.DataSource = null;
            grid.Rebind();
        }

        private void ItemCommand_AddSongToPlayList(Telerik.Web.UI.GridCommandEventArgs e)
        {
            DancersDBBL dancersDBBL = new DancersDBBL();
            GridDataItem item = e.Item as GridDataItem;
            int songID = int.Parse(item[StringConstants.SongID].Text);
            DancersDBData dancersDBData = dancersDBBL.GetSongForSongID(songID);

            if (Session[SessionConstants.DancersDBPlayListData] == null) // Create a new list and add the Song to it.
            {
                List<DancersDBData> dancersData = new List<DancersDBData>();
                dancersData.Add(dancersDBData);
                Session[SessionConstants.DancersDBPlayListData] = dancersData;
            }
            else // Add the song to the existing list
            {
                ((List<DancersDBData>)Session[SessionConstants.DancersDBPlayListData]).Add(dancersDBData);
            }
            ItemCommand_RebindGrid(rgPlayListData, "");
        }

        private void ItemCommand_RemoveSongFromPlayList(Telerik.Web.UI.GridCommandEventArgs e)
        {
            DancersDBBL dancersDBBL = new DancersDBBL();
            GridDataItem item = e.Item as GridDataItem;
            int songID = int.Parse(item[StringConstants.SongID].Text);

            if (Session[SessionConstants.DancersDBPlayListData] != null)
            {
                if (((List<DancersDBData>)Session[SessionConstants.DancersDBPlayListData]).Count > 0) // Only remove is there are songs in the list
                {
                    ((List<DancersDBData>)Session[SessionConstants.DancersDBPlayListData]).RemoveAll(songs => songs.SongID == songID);

                    ItemCommand_RebindGrid(rgPlayListData, "");
                }
            }
        }

        private void ConfigureExport()
        {
            rgPlayListData.ExportSettings.FileName = "Playlist";
            rgPlayListData.ExportSettings.ExportOnlyData = true;
            rgPlayListData.ExportSettings.IgnorePaging = true;
            rgPlayListData.ExportSettings.OpenInNewWindow = true;
            rgPlayListData.ExportSettings.UseItemStyles = false;

            rgPlayListData.MasterTableView.Columns[0].Visible = false;
            foreach(GridColumn column in rgPlayListData.MasterTableView.Columns)
            {
                column.Resizable = true;
                column.HeaderStyle.Font.Bold = false;
                column.HeaderStyle.Width = Unit.Pixel(60);
                column.HeaderText = column.HeaderText.Trim();
            }
            
        }
        #endregion

        #region Event Handlers
        // rgDanceData Event Handlers
        protected void rgDanceLibraryData_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e) // COME BACK AND FIX SESSION CONSTANTS
        {
            if (Session[SessionConstants.DancersDBLibraryData] != null)
            {
                rgDanceLibraryData.DataSource = (List<DancersDBData>)Session[SessionConstants.DancersDBLibraryData];
            }
            else
            {
                DancersDBBL dancersDBBL = new DancersDBBL();
                Session[SessionConstants.DancersDBLibraryData] = dancersDBBL.GetAllSongs();
                rgDanceLibraryData.DataSource = (List<DancersDBData>)Session[SessionConstants.DancersDBLibraryData];
            }
        }

        protected void rgDanceLibraryData_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case StringConstants.AddSong:
                    {
                        ItemCommand_AddSongToPlayList(e);
                        break;
                    }
                case StringConstants.RebindGrid:
                    {
                        ItemCommand_RebindGrid(rgDanceLibraryData, StringConstants.rcbDanceFilter);
                        break;
                    }
            }

        }

        protected void rgDanceLibraryData_PreRender(object sender, EventArgs e)
        {
            Initialize_rcbDanceStyleFilter(rgDanceLibraryData, StringConstants.rcbDanceFilter);
        }

        // rgPlayListData Event Handlers
        protected void rgPlayListData_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e) // COME BACK AND FIX SESSION CONSTANTS
        {
            if (Session[SessionConstants.DancersDBPlayListData] != null)
            {
                rgPlayListData.DataSource = (List<DancersDBData>)Session[SessionConstants.DancersDBPlayListData];
            }
        }

        protected void rgPlayListData_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                // NEED TO ADD EXPORT TO PDF and EXPORT TO GROOVESHARK (PENDING API APPROVAL)
                case StringConstants.RemoveSong:
                    {
                        ItemCommand_RemoveSongFromPlayList(e);
                        break;
                    }
            }

        }

        protected void rgPlayListData_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridHeaderItem && isExport)
            {
                foreach (TableCell cell in e.Item.Cells)
                {
                    cell.Style.Add("text-align", "left");
                }
                isExport = false;
            }
        }

        protected void btnExportWord_Click(object sender, ImageClickEventArgs e)
        {
            if(rgPlayListData.Items.Count > 0)
            {
                isExport = true;
                ConfigureExport();
                rgPlayListData.MasterTableView.ExportToWord();
            }
            else
            {
                rwmEmptyPlaylist.RadAlert("Songs need to be added to the playlist before it can be exported", 330, 180, "Empty playlist.", "");
            }
        }
        #endregion
    }
}